// 服务预约
import { getProductList } from "../../../../services/index";
import {request} from '../../../../utils/http'


import {isEmptyObject} from "../../../../utils/utils"
interface IServiceViewProps {
  showTitle: boolean
  className: string
}
const app = getApp()
interface IServiceViewData {
  curTypeIndex?: number;
  activeTab?: number;
  count?: number;
  menuTypeList?: {
    title: string;
    code: string;
  }[];
  dataItems?: {
    key: string;
    anchor: string;
    [props: string]: unknown;
    list: Project.IProjectItem[];
  }[];
}
interface IStaffViewMethods {
  tabItemClick(e): void;
}

Component<any, any, any>({
  data: {
    menuTypeList: [
      {
        title: "门店项目",
        code: "all"
      }
    ],
    curTypeIndex: -1,
    activeTab: 0,
    dataItems: [],
    count: 0,
    active:''
  },
  props: {
    className: '',
    showTitle: true,
    id:'',
  },
  onInit() {
    this.initData();
  },
  methods: {
    selectedPro(e){
      let { item }  = e.currentTarget.dataset
      console.log(item)
      let selectItems =  [item];
      this.allSelectItems = selectItems;

      this.setData({
        active:item.serviceId
      }, () => {
        this.setCount()
      })
    },
    getList(){
      request('/appointment/waiterProjects', 'get', {
        waiter_id:this.props.id
      }).then((res:any) => {
        if (res.code == 200) {
          let list = []
          res.data.forEach((item:any) =>{
            item.children.forEach((sub:any) => {
              list.push({
                serviceCode: item.id,
                serviceDesc: sub.name,
                serviceId: sub.id,
                serviceImg: sub.image,
                // serviceImg:'',
                serviceName: item.name,
                selected:true,
                serviceTime: sub.duration
              })
            })
          })

          const newData = list.reduce((curItems, item, index, items) => {
            if (this.judgeValue(curItems, item.serviceCode)) {
              let newItem = {
                key: item.serviceId,
                anchor: item.serviceCode,
                serviceName: item.serviceName,
                serviceCode: item.serviceCode,
                title: item.serviceName,
                list: this.getFilterItemsByCode({
                  value: item.serviceCode,
                  items
                })
              };
              curItems.push(newItem);
            }
            return curItems;
          }, []);
          console.log("getProductList::VtabItems:", newData);
          this.setData({
            dataItems: newData
          });
        }
      })
    },
    initData() {
      this.getList()
      // getProductList().then((res: Project.IProjectItem[]) => {
      //   console.log("getProductList::", res);
      //   this.allDataItems = res || [];
      //   if (res && res.length) {
      //     const newData = res.reduce((curItems, item, index, items) => {
      //       if (this.judgeValue(curItems, item.serviceCode)) {
      //         let newItem = {
      //           key: item.serviceId,
      //           anchor: item.serviceCode,
      //           serviceName: item.serviceName,
      //           serviceCode: item.serviceCode,
      //           title: item.serviceName,
      //           list: this.getFilterItemsByCode({
      //             value: item.serviceCode,
      //             items
      //           })
      //         };
      //         curItems.push(newItem);
      //       }
      //       return curItems;
      //     }, []);
      //     console.log("getProductList::VtabItems:", newData);
      //     this.setData({
      //       dataItems: newData
      //     });
      //   }
      // });
    },
    setCount() {
      this.setData({
        count: this.allSelectItems?.length || 0
      });
    },
    // 增加商品
    setAddValue({ cnt, item }) {
      let selectItems = this.allSelectItems || [];
      const addItems = [];
      for (let i = 0; i < cnt; i++) {
        addItems.push(item);
      }
      if (this.judgeValue(selectItems, item.serviceId, 'serviceId')) {
        selectItems = selectItems.concat(addItems);
      } else {
        const curId = item.serviceId;
        const notSelectItems = selectItems.filter(
          ele => ele.serviceId !== curId
        );
        selectItems = notSelectItems.concat(addItems);
      }
      this.allSelectItems = selectItems;
      this.setCount();
      console.log("setAddValue::", this.allSelectItems);
    },
    // 减少商品
    setSubValue({ item }) {
      const curId = item.serviceId;
      const notCurSelectItems = this.allSelectItems.filter(
        ele => ele.serviceId !== curId
      );
      this.allSelectItems = notCurSelectItems;
      this.setCount();
      console.log("setSubVale::", this.allSelectItems);
    },
    handleChange(value) {
      console.log("handleChange::stepValue:", value);
      if (value.cnt > 0) {
        this.setAddValue(value);
      } else {
        this.setSubValue(value);
      }
    },
    handlNext() {
      if (!this.allSelectItems?.length) {
        my.showToast({
          content: '请选择预约项目'
        })
        return
      }
      app.tempData.resServiceInfo = this.allSelectItems
      const {resTimeInfo, resStaffInfo} = app.tempData
      if (!isEmptyObject(resStaffInfo) && !isEmptyObject(resTimeInfo)) {
        // TODO 确认页
        my.navigateTo({
          url: '/pages/reservation/confirm-index/index'
        })
      } else {
        // TODO 员工预约
        my.navigateTo({
          url: '/pages/reservation/staff-index/index'
        })
      }
      
    },
    judgeValue(curItems = [], value, keyName='serviceCode') {
      if (curItems && curItems.length) {
        const keyItems = curItems.map(item => item[keyName]);
        if (keyItems.includes(value)) {
          return false;
        } else {
          return true;
        }
      } else {
        return true;
      }
    },
    getFilterItemsByCode({ value, items }) {
      return items.filter(item => value === item.serviceCode);
    },
    onTabChange(index) {
      this.setData({
        activeTab: index
      });
    }
  }
});
